package com.xgp.labblogaffter.Controller;

import com.xgp.labblogaffter.mapper.WritesMapper;
import com.xgp.labblogaffter.mapper.WritesMapperExt;
import com.xgp.labblogaffter.model.Writes;
import com.xgp.labblogaffter.model.WritesExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class WritesController {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    WritesMapper writesMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    WritesMapperExt writesMapperExt;

    /**
     * 查找所有的文章信息
     * @return
     */
    @GetMapping("/findAllWrites")
    public List<Writes> findAllWrites() {

        return writesMapperExt.findAll();
    }

    /**
     * 按时间顺序查找文章
     * @return
     */
    @GetMapping("/findAllWritesByTime")
    public List<Writes> findAllWritesByTime() {
        try {

            WritesExample writesExample = new WritesExample();
            writesExample.setOrderByClause("time desc");

            return writesMapper.selectByExample(writesExample);
        }catch (Exception e) {
            return null;
        }

    }

    /**
     * 根据id查询文章
     * @param id
     * @return
     */
    @PostMapping("/findWritesById")
    public Writes findWritesById(String id) {
        try {
            return writesMapper.selectByPrimaryKey(Integer.parseInt(id));
        }catch (Exception e) {
            return null;
        }
    }

    /**
     * 添加文章
     * @param title
     * @param text
     * @return
     */
    @PostMapping("/addWrites")
    public boolean addWrites(String title,String text) {
        try {
            Writes writes = new Writes();
            writes.setTitle(title);
            writes.setText(text);
            writes.setTime(System.currentTimeMillis());
            int flag = writesMapper.insertSelective(writes);
            if(flag == 1) {
                return true;
            }
            return false;
        }catch (Exception e) {
            return false;
        }

    }

    /**
     * 放回文章数量
     * @return
     */
    @GetMapping("/WritesCount")
    public long WritesCount() {
        try {
            return writesMapperExt.WritesCount();
        }catch (Exception e) {
            return 0;
        }
    }


    /**
     * 根据id来修改文章
     * @param wid
     * @param title
     * @param text
     * @return
     */
    @PostMapping("/updateWritesById")
    public boolean updateWritesById(String wid,String title ,String text) {
        try {
            Writes writes = new Writes();
            writes.setWid(Integer.parseInt(wid));
            writes.setText(text);
            writes.setTitle(title);
            writes.setTime(System.currentTimeMillis());
            int flag = writesMapper.updateByPrimaryKeySelective(writes);
            if(flag == 1) {
                return true;
            }
            return false;
        }catch (Exception e) {
            return false;
        }

    }

    /**
     * 根据id删除文章
     * @param wid
     * @return
     */
    @PostMapping("/deleteWritesById")
    public boolean deleteWritesById(String wid) {
        try{
            if(writesMapper.deleteByPrimaryKey(Integer.parseInt(wid)) == 1) {
                return true;
            }
            return false;
        }catch (Exception e) {
            return false;
        }
    }
}
